package com.yycx.msg.provider.configuration;


import cn.hutool.core.map.MapUtil;
import com.yycx.common.base.entity.EntityMap;
import com.yycx.common.utils.RedisUtils;
import com.yycx.msg.provider.service.impl.HuaWeiSmsSenderImpl;
import com.yycx.msg.provider.service.impl.TencentSmsSenderImpl;
import com.yycx.msg.provider.sms.SmsSender;
import com.yycx.msg.provider.service.impl.AliyunSmsSenderImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author zyf
 */
@Configuration
@Slf4j
public class SmsConfiguration {

    @Bean
    public SmsSender smsSender(RedisUtils redisUtils) {
        SmsProperties smsProperties = new SmsProperties();
        EntityMap map = redisUtils.getConfigMap("SMS_CONFIG");
        String accessKey = map.get("accessKey");
        String accessSecret = map.get("accessSecret");
        String signName = map.get("signName");
        String smsTplCode = map.get("smsTplCode");
        String appId = map.get("appId");
        //短信服务商
        String serviceName = MapUtil.getStr(map, "serviceName", "aliyun");
        smsProperties.setAccessKeyId(accessKey);
        smsProperties.setAccessKeySecret(accessSecret);
        smsProperties.setSignName(signName);
        smsProperties.setSmsTplCode(smsTplCode);
        smsProperties.setAppId(appId);
        SmsSender sender = null;
        if (serviceName.equals("aliyun")) {
            sender = new AliyunSmsSenderImpl(smsProperties);
        }
        if (serviceName.equals("huawei")) {
            sender = new HuaWeiSmsSenderImpl(smsProperties);
        }
        if (serviceName.equals("tencent")) {
            sender = new TencentSmsSenderImpl(smsProperties);
        }
        return sender;
    }

}
